#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#define FilePath "./xnfile"

int main(int argc, char *argv[])
{
	int res;
	int fd;
	size_t num;
	char str[] = "123456789";
	char readBuf[2048] = "";

	fd = open(FilePath, O_RDWR | O_CREAT | O_TRUNC, 0666); 
	if (fd == -1) {
		perror("open error");
		return 1;
	}
	printf("open %s success\n", FilePath);

	res = posix_fallocate(fd, 0, 1024);
	if(res < 0 ){
		printf("res = %d, errno = %d,  %s\n", res, errno, strerror(errno));
		return 1;
	}
	printf("create sparefile size is 1024\n");

	if (-1 == lseek(fd, 1024, SEEK_SET)) {
		perror("lseek error");
		return 1;
	}

	num = write(fd, str, strlen(str));
	if (num < strlen(str)) {
		perror("write less or error\n");
		return 1;
	}
	printf("lseek 1024 and then write %d bytes success\n", num);

	if (-1 == lseek(fd, 1024, SEEK_SET)) {
		perror("lseek error");
		return 1;
	}

	num = read(fd, readBuf, 10);
	if (num < 0) {
		perror("read error\n");
		return 1;
	}

	printf("read:[%s]\n", readBuf);

	return 0;
}

